<?php
	ob_start();

	define('BASE_FRAMEWORK', dirname(__FILE__));
	define('APP_PATH',       dirname($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']));
    //define('APP_PATH', 'C:/wamp/www/lareina/admin');
    //define('APP_PATH', 'C:/wamp/www/lareina/admin/AdminClass');

	include_once(APP_PATH . '/Configuracion.inc.php');
	if (DB_ACTIVE)
	{
		include(BASE_FRAMEWORK . '/Connection.inc.php');
	}
	include_once(BASE_FRAMEWORK . '/Login.class.php');
	include_once(BASE_FRAMEWORK . '/Seccion.class.php');
	include_once(APP_PATH       . '/Cargar_Admin.inc.php');
	include_once(BASE_FRAMEWORK . '/Actualizacion.class.php');
	include_once(BASE_FRAMEWORK . "/SQL.class.php");
	include_once(BASE_FRAMEWORK . "/Apeform.class.php");

	$generator = ucfirst(strtolower($_REQUEST['generator']));
	$page   = ucfirst(strtolower($_REQUEST['page']));
	$action = ucfirst(strtolower($_REQUEST['action']));

	if ((empty($page) && !empty($_ADMIN->Menu->default)) && (!GENERATOR_ACTIVE || empty($generator)))
	{
		header("Location: index.php" . $_ADMIN->Menu->default);
		exit();
	}

	if (GENERATOR_ACTIVE && !empty($generator))
	{
		$file   = BASE_FRAMEWORK . "/Generator/" . $generator . $action . ".php";
		$_ADMIN->Titulo_Seccion = $generator;
	} else {
		$file   = APP_PATH . "/" . $page . $action . ".php";
		$_ADMIN->Titulo_Seccion = $page;
	}
    $login = new Login();
	if (!$login->SessionExpire())
	{
		$login->Get_Session();

		if (TIPOS_ADMIN)
		{
			$campo_pk = $login->login_attributos['campo_pk'];
            $_ADMIN->Menu->id_usuario = $login->$campo_pk;

			if (empty($_SESSION['TIPOS_ADMIN'][$login->$campo_pk]))
			{
				$sql = "SELECT seccion FROM ad_secciones WHERE ".$login->login_attributos['campo_pk']." = '".$login->$campo_pk."'";
				$query = mysql_query($sql, $conn);

				while($row = mysql_fetch_object($query))
				{
					$_SESSION['TIPOS_ADMIN'][$login->$campo_pk][$row->seccion] = $row->seccion;
				}
			}
            if ($page != 'Login' && $page != 'Valida' && $page != 'Logout')
            {
                $keys = array_keys($_SESSION['TIPOS_ADMIN'][$login->$campo_pk]);
                if (!empty($page) && !in_array('*', $keys) && !in_array($page, $keys))
                {
                    $page = $keys[0];
                    $file = APP_PATH . "/" . $page . $action . ".php";
                    $_ADMIN->Titulo_Seccion = $page;
                }
			    if ($_SESSION['TIPOS_ADMIN'][$login->$campo_pk][$page] != $page && $_SESSION['TIPOS_ADMIN'][$login->$campo_pk]['*'] != '*')
			    {
				    header('Location: index.php');
				    exit();
			    }
            }
		}

		$_ADMIN->Header();
		if (file_exists($file))
		{
			include($file);
		} elseif (!empty($page)) {
			echo "ARCHIVO NO ENCONTRADO $file!";
		}

		$_ADMIN->Footer();
	} else {
		$file   = APP_PATH . "/Login.php";
		if (!empty($page) && $page == 'Valida')
		{
			$file   = APP_PATH . "/Valida.php";

		} elseif (!empty($page) && $page != 'Login')
		{
			$login->Login_Error();
		}
		include($file);
	}

	ob_end_flush();
?>